﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class ImageController : MonoBehaviour {

    public RawImage img = null;
    public Transform parentTrans = null;
    


    private static ImageController instance = null;
    public static ImageController GetInstance()
    { return instance; }

    private void Awake()
    {
        instance = this;
    }

    private void Start()
    {
        gameObject.SetActive(false);
    }

    public void Open( List<string> li  )
    {
        Loading.GetInstance().StartLoading();

        Resources.UnloadUnusedAssets();
        gameObject.SetActive(true);

        int count = li.Count - parentTrans.childCount;
        if ( count > 0 )
        {
            for (int i = 0; i < count; i++ )
            {
                GameObject go = Instantiate(img.gameObject) as GameObject;
                go.transform.parent = parentTrans;
            }

        }
        else if ( count < 0 )
        {
            for ( int i = parentTrans.childCount - 1; i > li.Count + count; i-- )
            {
                GameObject.Destroy(parentTrans.GetChild(i).gameObject);
            }
        }

        int DownCount = 0;
        for( int i = 0; i < li.Count; i++ )
        {
            RawImage im = parentTrans.GetChild(i).gameObject.GetComponent<RawImage>();
            ResLoader.Load(li[i], null, (www) => {

                im.texture = www.texture;

                RectTransform rt = im.gameObject.GetComponent<RectTransform>();

                rt.sizeDelta = new Vector2(www.texture.width * (Screen.height*1f / www.texture.height), Screen.height);

                DownCount++;
                if ( DownCount >= li.Count )
                {
                    Loading.GetInstance().EndLoading();
                }
            },
        true, true);
        }

    }

    public void Clear()
    {

        for( int i = 1; i < parentTrans.childCount; i++ )
        {
            Transform t = parentTrans.GetChild(i);
            GameObject.Destroy(t.gameObject);

        }

    }


    public void Open(string url = null, bool clean = true)
    {
        Resources.UnloadUnusedAssets();
        if (clean)
            img.texture = null;

        gameObject.SetActive(true);

        if (string.IsNullOrEmpty(url))
            return;




        ResLoader.Load(url, null, (www) => {

#if UNITY_IPHONE
            Texture2D tex = new Texture2D(8196, 8196);
            
            //            tex.LoadImage(www.texture.EncodeToJPG());
            www.LoadImageIntoTexture(tex);
            //tex.Compress(false);
            Debug.Log("www.texture " + www.texture.width + "," + www.texture.height + "    last:" + tex.width + "," + tex.height);
            img.texture = tex;
#else
            img.texture = www.texture;
#endif

            RectTransform rt = img.gameObject.GetComponent<RectTransform>();


            rt.sizeDelta = new Vector2(www.texture.width*( 1080f/www.texture.height),1080);


        },
        true, true);
    }


    public void OnClose()
    {
        gameObject.SetActive(false);
    }

}
